package refresolver

/*
Copyright 2021-2025 The k8gb Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"fmt"
	"testing"

	k8gbv1beta1 "github.com/k8gb-io/k8gb/api/v1beta1"
	utils "github.com/k8gb-io/k8gb/controllers/utils"
	"github.com/stretchr/testify/assert"
	istio "istio.io/client-go/pkg/apis/networking/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/kubernetes/scheme"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
	gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

// TestNew tests if the RefResolver is instantiated with the correct type
func TestNew(t *testing.T) {
	var tests = []struct {
		name                          string
		gslbYaml                      string
		expectedReferenceResolverType string
		expectedError                 error
	}{
		{
			name:                          "embedded ingress",
			gslbYaml:                      "./testdata/gslb_ingress_embedded.yaml",
			expectedReferenceResolverType: "*ingress.ReferenceResolver",
			expectedError:                 nil,
		},
		{
			name:                          "referenced ingress",
			gslbYaml:                      "./testdata/gslb_ingress_referenced.yaml",
			expectedReferenceResolverType: "*ingress.ReferenceResolver",
			expectedError:                 nil,
		},
		{
			name:                          "referenced istio",
			gslbYaml:                      "./testdata/gslb_istio.yaml",
			expectedReferenceResolverType: "*istiovirtualservice.ReferenceResolver",
			expectedError:                 nil,
		},
		{
			// only the embedded resolver will be picked up
			name:                          "referenced and embedded ingress",
			gslbYaml:                      "./testdata/gslb_ingress_referenced_and_embedded.yaml",
			expectedReferenceResolverType: "*ingress.ReferenceResolver",
			expectedError:                 nil,
		},
		{
			name:                          "referenced gateway API HTTPRoute",
			gslbYaml:                      "./testdata/gslb_gatewayapi_httproute.yaml",
			expectedReferenceResolverType: "*gatewayapihttproute.ReferenceResolver",
			expectedError:                 nil,
		},
		{
			name:                          "referenced gateway API GRPCRoute",
			gslbYaml:                      "./testdata/gslb_gatewayapi_grpcroute.yaml",
			expectedReferenceResolverType: "*gatewayapigrpcroute.ReferenceResolver",
			expectedError:                 nil,
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// arrange
			cl, gslb := getTestContext(test.gslbYaml)

			// act
			resolver, err := New(gslb, cl)

			// assert
			assert.Equal(t, test.expectedError, err)
			if err == nil {
				assert.Equal(t, test.expectedReferenceResolverType, fmt.Sprintf("%T", resolver))
			}
		})
	}
}

// getTextContext creates a kubernetes client with access to all objects that need to be looked up
func getTestContext(gslbFile string) (client.Client, *k8gbv1beta1.Gslb) {
	gslb := utils.FileToGSLB(gslbFile)
	objs := []runtime.Object{
		gslb,
		utils.FileToIngress("./testdata/ingress_referenced.yaml"),
		utils.FileToIngress("./testdata/ingress_embedded.yaml"),
		utils.FileToIstioVirtualService("./testdata/istio_virtualservice.yaml"),
		utils.FileToIstioGateway("./testdata/istio_gateway.yaml"),
		utils.FileToGatewayAPIHTTPRoute("./testdata/gatewayapi_httproute.yaml"),
		utils.FileToGatewayAPIGRPCRoute("./testdata/gatewayapi_grpcroute.yaml"),
		utils.FileToGatewayAPIGateway("./testdata/gatewayapi_gateway.yaml"),
		utils.FileToService("./testdata/istio_service.yaml"),
	}
	// Register operator types with the runtime scheme.
	s := scheme.Scheme
	s.AddKnownTypes(k8gbv1beta1.GroupVersion, &k8gbv1beta1.Gslb{}, &k8gbv1beta1.GslbList{})
	s.AddKnownTypes(istio.SchemeGroupVersion, &istio.VirtualService{}, &istio.VirtualServiceList{})
	s.AddKnownTypes(istio.SchemeGroupVersion, &istio.Gateway{}, &istio.GatewayList{})
	s.AddKnownTypes(gatewayapiv1.SchemeGroupVersion, &gatewayapiv1.HTTPRoute{}, &gatewayapiv1.HTTPRouteList{})
	s.AddKnownTypes(gatewayapiv1.SchemeGroupVersion, &gatewayapiv1.GRPCRoute{}, &gatewayapiv1.GRPCRouteList{})
	s.AddKnownTypes(gatewayapiv1.SchemeGroupVersion, &gatewayapiv1.Gateway{}, &gatewayapiv1.GatewayList{})
	cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objs...).Build()

	return cl, gslb
}
